package urlconfig;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.TimeZone;

/**
 * @author 01395265
 * @date 2020/7/27
 */
public class TestTimeZone {

    public static void main(String[] args) throws Exception {
        TimeZone.setDefault(TimeZone.getTimeZone("GMT+6:00"));

        Connection connection = getConnection();

//        System.out.println(ZoneId.of("CST"));

        System.out.println("当前JVM时区：" + ZoneId.systemDefault());

//        modifySessionStatus(connection);
        connectionShow(connection);
        insert(connection);
        query(connection);
        closeConnection(connection);
    }

    public static void modifySessionStatus(Connection connection) throws Exception {
        PreparedStatement namesPs = connection.prepareStatement("set session time_zone='+0:00';");
        namesPs.executeUpdate();
    }

    public static void connectionShow(Connection connection) throws Exception {
        String sql = "show session variables like 'time_zone'";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            String date_time = (String) resultSet.getObject("Variable_name");
            String time_stamp = (String) resultSet.getObject("Value");
            System.out.println("当前MySQL连接时区：" + date_time + " = " + time_stamp);
        }
    }

    public static void query(Connection connection) throws Exception {
        String sql = "select * from DATE_URL_TEST";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            LocalDateTime date_time = (LocalDateTime) resultSet.getObject("date_time");
            Timestamp time_stamp = (Timestamp) resultSet.getObject("time_stamp");
            System.out.println(date_time + ", " + time_stamp);
        }
    }

    public static void insert(Connection connection) throws Exception {
        String sql = "insert into date_url_test(date_time, time_stamp) values (?, ?)";
        PreparedStatement statement = connection.prepareStatement(sql);

        System.out.println(LocalDateTime.now());
        LocalDateTime parse = LocalDateTime.parse("2020-01-02T01:00:00");
        System.out.println("待插入的时间：" + parse);

        statement.setTimestamp(1, Timestamp.valueOf(parse));
        statement.setTimestamp(2, Timestamp.valueOf(parse));
        int i = statement.executeUpdate();
        if(i <= 0) {
            System.out.println("插入数据失败！");
        }
    }

    private static Connection getConnection() {
        String jdbcUrl = "jdbc:mysql://10.88.26.129:3307/auth?preserveInstants=true&connectionTimeZone=GMT%2B7&forceConnectionTimeZoneToSession=true";
        try {
            DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        try {
            return DriverManager.getConnection(jdbcUrl, "authopr", "authopr#d3");
        } catch (SQLException exception) {
            exception.printStackTrace();
        }
        return null;
    }

    private static void closeConnection(Connection connection) {
        try {
            connection.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

}
